
//jinzilong add start 20120327
package com.android.matmanual;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.media.AudioManager;
import android.content.Context;

public class SpeakerTest extends BaseActivity{
    /** Called when the activity is first created. */
	TextView tv;
	MediaPlayer mMp;
	WakeLock mWakeLock;
	AudioManager audioManager = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.speaker_test);
        setTitle(R.string.Speaker_name);
        
        PowerManager pm =
 	           (PowerManager) getSystemService(POWER_SERVICE); 
 	    mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, 
 	          "XYTEST"); 
 	    mWakeLock.acquire(); 


 	    audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);  
	//	for(int i=0; i<5; i++)
	//		audioManager.setMode(1);
		while(audioManager.getMode() != 1)
		{
			Log.v("lifei-MatManual", "##################### audioManager.getMode try to set RING");
			audioManager.setMode(1);
		}
		Log.v("lifei-MatManual", "##################### audioManager.getMode:"+audioManager.getMode());
		audioManager.setMicrophoneMute(false);          

        tv = (TextView)findViewById(R.id.tv);
        tv.setText(R.string.speaker_open);


		// TODO Auto-generated method stub
		try{
			if(mMp == null)
			{
		       mMp = new MediaPlayer();
		       mMp = MediaPlayer.create(SpeakerTest.this,R.raw.hurt);
			   mMp.stop();
				
			   mMp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
					
					public void onCompletion(MediaPlayer mp) {
						// TODO Auto-generated method stub
						try{
							mMp.stop();
							mMp.release();
							mMp = null;
							tv.setText(R.string.speaker_closed);
						}catch (Exception e) {
							// TODO: handle exception
							tv.setText(e.toString());
							e.printStackTrace();
						}
						
					}
				});
		        mMp.setOnErrorListener(new MediaPlayer.OnErrorListener() {
					
					public boolean onError(MediaPlayer mp, int what, int extra) {
						// TODO Auto-generated method stub
						try{
							
						}catch (Exception e) {
							// TODO: handle exception
							tv.setText(e.toString());
							e.printStackTrace();
						}
						return false;
					}
				});
				mMp.prepare();
				mMp.start();
				tv.setText(R.string.speaker_open);
			}
			
		}catch (Exception e) {
			// TODO: handle exception
			tv.setText(e.toString());
			e.printStackTrace();
		}
		
	
          
     
    }  


    @Override
    protected void onDestroy() {
    	// TODO Auto-generated method stub
    	try{
    		if(mMp!=null)
    		{
    			mMp.stop();
        		mMp.release();	
        		mMp = null;
    		}
    	}catch (Exception e) {
			// TODO: handle exception
    		tv.setText(e.toString());
    		e.printStackTrace();
		}
		if(mWakeLock!=null)
		{
			mWakeLock.release(); 
			mWakeLock = null;
		}
    	super.onDestroy();
    }
    
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
	//	audioManager.setMode(AudioManager.MODE_IN_CALL);
        finish();
		super.onStop();
	}
	@Override
	public void onAttachedToWindow() 
	{
		// TODO Auto-generated method stub
		super.onAttachedToWindow();
		this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		int result = TestControl.GetKeyEvent(keyCode);
		if (result != TestControl.DO_NOTHING) {
			setResult(result);
			finish();
	    }
	    return true;
	}	
}
//jinzilong end 
		 